package com.nuaa.yupicturebackend.test;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Setter
@Getter
class InfoCollections {
	// Getter and Setter
	private String answer;

}

public class FilterInfoCollections {

	public static List<InfoCollections> filterCollections(String jsonData) {
		Gson gson = new Gson();
		Type collectionType = new TypeToken<List<InfoCollections>>() {
		}.getType();

		// 将json字符串转换为List<InfoCollections>  账号  扫描时间 登录时间
		List<InfoCollections> collectionsList = gson.fromJson(jsonData, collectionType);

		Set<String> seenAnswers = new HashSet<>();
		List<InfoCollections> resultList = new ArrayList<>();

		for (InfoCollections item : collectionsList) {
			// 使用中文顿号和逗号分割答案
			String[] answers = item.getAnswer().split("[,、]");
			if (answers.length > 1) {
				// 如果有多个答案，加入到seenAnswers集合
				for (String ans : answers) {
					seenAnswers.add(ans.trim());
				}
				resultList.add(item);
			} else {
				// 单个答案判断是否已经存在
				if (!seenAnswers.contains(answers[0].trim())) {
					seenAnswers.add(answers[0].trim());
					resultList.add(item);
				}
			}
		}

		return resultList;
	}

	public static void main(String[] args) {
		String jsonInput = "[{\"answer\":\"C、C++\"},{\"answer\":\"Android、操作系统\"},{\"answer\":\"TCP/UDP\"},{\"answer\":\"iOS App\"},{\"answer\":\"Qt\"},{\"answer\":\"Android\"},{\"answer\":\"C/C++\"}]";
		// 提取info_collections数组部分以匹配我们的模型
		jsonInput = jsonInput.replace("{\"info_collections\":", "").replaceAll("}$", "");

		List<InfoCollections> filteredList = filterCollections(jsonInput);

		// 输出结果
		for (InfoCollections ic : filteredList) {
			System.out.println(ic.getAnswer());
		}
	}
}